package com.yiqi.liebang.feature.setting.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.RegisterBo;
import com.yiqi.liebang.framework.api.UserApi;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class WithdrawBidingPhone extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_edit_pwds)
    TextView tvEditPwds;
    @BindView(R.id.view_edit_pwds)
    View viewEditPwds;
    @BindView(R.id.edt_edit_pwd_phones)
    EditText edtEditPwdPhones;
    @BindView(R.id.btn_edit_pwd_codes)
    TextView btnEditPwdCodes;
    @BindView(R.id.edt_edit_pwd_codes)
    EditText edtEditPwdCodes;
    @BindView(R.id.btn_pwd_commit)
    TextView btnPwdCommit;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_change_phone;
    }

    @Override
    protected void initInjector() {

    }
    int payType;
    double money;
    @Override
    protected void initData() {
        initToolBar(toolbar, "验证手机号码", true, true);
        payType=getIntent().getIntExtra("payType",0);
        money=getIntent().getDoubleExtra("money",0);

        btnEditPwdCodes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ValidatorUtil.isMobile(edtEditPwdPhones.getText().toString())){
                    showMsg("请输入正确手机号！");
                    return;
                }
                AM.api().createApiService(UserApi.class).sendCode(new RegisterBo(edtEditPwdPhones.getText().toString()))
                        .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                        .subscribe(new Observer<RegisterBo>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                showLoading(d);
                            }

                            @Override
                            public void onNext(RegisterBo friendBeans) {
                                T.showShort("验证获取成功");
                                dismissLoading();
                                onGetCode();
                            }

                            @Override
                            public void onError(Throwable e) {
                                T.showShort("获取失败");
                                dismissLoading();
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });
        btnPwdCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AM.api().createApiService(UserApi.class).verificationUser(new RegisterBo(edtEditPwdPhones.getText().toString(),
                        edtEditPwdCodes.getText().toString(),""))
                        .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                showLoading(d);
                            }

                            @Override
                            public void onNext(String friendBeans) {
                                dismissLoading();
                                if(payType==1){
                                    Intent intent=new Intent(getApplicationContext(),BindingWeChatPay.class);
                                    startActivityForResult(intent,30001);
                                }else{
                                    Intent intent=new Intent(getApplicationContext(),AlibbActivity.class);
                                    intent.putExtra("money",money);
                                    startActivityForResult(intent,60001);
                                }

                            }

                            @Override
                            public void onError(Throwable e) {
                                T.showShort(e.getMessage());
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==30001){
            if(data!=null&& !TextUtils.isEmpty(data.getStringExtra("openId"))){
                Intent intent=new Intent();
                intent.putExtra("openId",data.getStringExtra("openId"));
                setResult(30001,intent);
                finish();//微信
            }
        }else if(requestCode==60001){
            if(data!=null&& !TextUtils.isEmpty(data.getStringExtra("alibb"))){
                Intent intent=new Intent();
                intent.putExtra("alibb",data.getStringExtra("alibb"));
                setResult(60001,intent);
                finish();//微信
            }
        }
    }

    public void onGetCode() {
        final int count = 60;//倒计时10秒
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        btnEditPwdCodes.setEnabled(false);
                        btnEditPwdCodes.setTextColor(AM.res().getColor(R.color.primary_highlight));
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {

                btnEditPwdCodes.setText(num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                //回复原来初始状态
                btnEditPwdCodes.setEnabled(true);
                btnEditPwdCodes.setText("发送验证码");
                btnEditPwdCodes.setTextColor(AM.res().getColor(R.color.text_primary));
            }
        });

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
